home *** CD-ROM | disk | FTP | other *** search
- /* File: Menus.c */
-
- #include <AppleEvents.h>
- #include "AERegistry.h"
- #include "AEObjects.h"
- #include "AEPackObject.h"
- #include "ScriptScrap.h"
- #include "Prototypes.h"
-
-
-
- void SetItemEnable (MenuHandle theMenu, short theItem, Boolean enabled)
- {
- if (enabled)
- EnableItem(theMenu, theItem);
- else
- DisableItem(theMenu, theItem);
- } /* SetItemEnable */
-
-
- void EnableFileCommands ()
- {
- MenuHandle fileMenu = GetMHandle(kFileMenu);
- WindowPtr inFront = FrontWindow();
-
- EnableItem (fileMenu, cNew);
- EnableItem (fileMenu, cOpen);
- SetItemEnable(fileMenu, cClose, (inFront != NULL) && (((WindowPeek)inFront)->goAwayFlag));
- } /* EnableFileCommands */
-
-
- void AdjustMenus(void)
- {
- WindowPtr inFront = FrontWindow();
- Boolean isAUserWindow, isDeskAcc;
-
- EnableFileCommands();
- DisableItem(GetMHandle(kEditMenu), 0);
- if ((inFront) && (((WindowPeek)inFront)->windowKind < 0))
- EnableItem(GetMHandle(kEditMenu), 0);
- else
- DisableItem(GetMHandle(kEditMenu), 0);
- } /* AdjustMenus */
-
-
-
- void DoAppleCmds (short theItem)
- {
- Str255 name; /* string for DA name */
-
- switch (theItem) {
-
- case cAbout:
- Alert(kAboutDialog, NULL);
- break;
-
- default:
- GetItem(GetMHandle(kAppleMenu), theItem, (StringPtr)&name);
- OpenDeskAcc((StringPtr)&name);
- }
- }
-
-
- void DoFileCmds (short theItem)
- {
- long result = 0;
- FSSpec theFile;
- SFTypeList typeList = {'scbk', ' ', ' ', ' '};
- StandardFileReply reply;
- OSErr err;
-
- switch (theItem) {
-
- case cNew:
- ae_do_NewDocument();
- break;
-
- case cOpen:
- StandardGetFile(NULL, 1, typeList, &reply);
- if (reply.sfGood)
- ae_do_Open(&reply.sfFile);
- break;
-
- case cClose:
- ae_do_Close();
- break;
-
- case cQuit:
- ae_do_Quit();
- }
- } /* DoFileCmds */
-
-
- void Dispatch (long menuResult)
- {
- short theMenu = (menuResult >> 16); /* menu selected */
- short theItem = (menuResult & 0x0000FFFF); /* item selected */
-
- switch (theMenu) {
-
- case kAppleMenu:
- DoAppleCmds(theItem);
- break;
-
- case kFileMenu:
- DoFileCmds(theItem);
- break;
-
- case kEditMenu:
- SystemEdit(theItem - 1);
- break;
- }
- HiliteMenu(0); /* un-hilite selected menu */
- }
-
-
- void ae_do_Quit (void)
- {
- /* Construct an Apple event to quit the application */
-
- OSErr err = noErr;
- AppleEvent anAppleEvent, replyAE;
- DescType saveOption;
-
- err = AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, &gSelfAddressDesc,
- kAutoGenerateReturnID, kAnyTransactionID,
- &anAppleEvent);
- if (err == noErr) {
- /* Send the event to ourselves */
- err = AESend (&anAppleEvent, &replyAE, kAENoReply + kAECanInteract,
- kAENormalPriority, kAEDefaultTimeout, (IdleProcPtr)0L,
- (EventFilterProcPtr)0L);
-
- }
- (void)AEDisposeDesc(&anAppleEvent);
- CheckResult(err, "ae_do_Quit");
- }
-
-
- void ae_do_Close ()
- {
- /* Construct an Apple event to close the frontmost window */
- /* The direct parameter is an object specifier for the window to be closed */
-
- OSErr err = noErr;
- AppleEvent anAppleEvent, replyAE;
- AEDesc objSpec,
- nullContainer = {'null', 0L},
- keyData;
- long index;
-
- if (FrontWindow() == NULL) return; /* Don't do the following if no windows are open */
-
- err = AECreateAppleEvent(kAECoreSuite, kAEClose, &gSelfAddressDesc,
- kAutoGenerateReturnID, kAnyTransactionID,
- &anAppleEvent);
- if (err != noErr) goto done;
-
- /* Create an object specifier for the frontmost window */
- index = 1; /* This is the frontmost window or document */
- (void)AECreateDesc(typeLongInteger, (Ptr)&index, sizeof(index), &keyData);
- err = CreateObjSpecifier(cWindow, &nullContainer, formAbsolutePosition, &keyData, true, &objSpec); /* This will dispose of the inputs for us */
- if (err != noErr) goto done;
-
- /* Put this object specifier into the direct parameter */
- err = AEPutParamDesc (&anAppleEvent, keyDirectObject, &objSpec); /* The object to be closed */
- (void)AEDisposeDesc (&objSpec);
- if (err != noErr) goto done;
-
- /* Send the event to ourselves */
- err = AESend (&anAppleEvent, &replyAE, kAENoReply,
- kAENormalPriority, kAEDefaultTimeout, (IdleProcPtr)0L,
- (EventFilterProcPtr)0L);
-
- done:
- (void)AEDisposeDesc(&anAppleEvent);
- CheckResult(err, "ae_do_Close");
- }
-
- void ae_do_Open (FSSpec *theFileSpec)
- {
- OSErr err;
- AppleEvent anAppleEvent, replyAE;
- AEDesc aliasDesc = {typeNull, NULL};
-
- /* Send an "open document {file-alias}" event back to this application */
- err = AECreateAppleEvent(kCoreEventClass, /* <- Note: this is what AppleEvents.h calls the 4required suite */
- kAEOpenDocuments, &gSelfAddressDesc,
- kAutoGenerateReturnID, kAnyTransactionID,
- &anAppleEvent);
- if (err != noErr) goto done;
- /* Make an alias for the file */
- err = NewAlias(NULL, theFileSpec, (AliasHandle*)&aliasDesc.dataHandle);
- if (err != noErr) goto done;
- aliasDesc.descriptorType = typeAlias;
-
- /* Add it to the event */
- err = AEPutParamDesc(&anAppleEvent, keyDirectObject, &aliasDesc); /* The file to be opened */
- if (err != noErr) goto done;
- err = AESend (&anAppleEvent, &replyAE, kAENoReply,
- kAENormalPriority, kAEDefaultTimeout, (IdleProcPtr)0L,
- (EventFilterProcPtr)0L);
-
- done:
- if (aliasDesc.dataHandle != NULL)
- (void) AEDisposeDesc(&aliasDesc);
- (void) AEDisposeDesc(&anAppleEvent);
- CheckResult(err, "ae_do_Open");
- }
-
- void ae_do_NewDocument ()
- {
- /* Code revised 1/22/92 to match the Winter '92 Registry */
- AppleEvent anAppleEvent = {typeNull, NULL}, replyAE;
- AERecord scratchRecord = {typeNull, NULL};
- AERecord insertionLoc = {typeNull, NULL};
- AEDesc nullDesc = { typeNull, NULL };
- DescType scratchType;
- OSErr err = noErr;
-
- /* Send a "Create Element {document from null}" event back to this application */
- err = AECreateAppleEvent(kAECoreSuite, kAECreateElement, &gSelfAddressDesc,
- kAutoGenerateReturnID, kAnyTransactionID,
- &anAppleEvent);
- CheckResult(err, "ae_do_NewDocument: New, creating Apple event");
-
- /* Create the typeInsertionLoc specifier, which consists of an object specifier and */
- /* a position within the container ("null", and "at the beginning", respectively.) */
- err = AECreateList(NULL, 0, true /* isRecord */, &scratchRecord);
- if (err != noErr) goto done;
- err = AEPutParamDesc(&scratchRecord, keyAEObject, &nullDesc);
- if (err != noErr) goto done;
- scratchType = kAEBeginning;
- err = AEPutParamPtr (&scratchRecord, keyAEPosition, typeEnumeration, (Ptr)&scratchType, sizeof(scratchType));
- if (err != noErr) goto done;
- err = AECoerceDesc(&scratchRecord, typeInsertionLoc, &insertionLoc);
- if (err != noErr) goto done;
-
- /* Place the InsertionLoc into the Apple event */
- err = AEPutParamDesc(&anAppleEvent, keyAEInsertHere, &insertionLoc); /* Where to put the window */
- if (err != noErr) goto done;
-
- scratchType = cDocument;
- (void)AEPutParamPtr(&anAppleEvent, keyAEObjectClass, typeType, (Ptr)&scratchType, sizeof(scratchType)); /* cWindow */
-
- err = AESend (&anAppleEvent, &replyAE, kAENoReply,
- kAENormalPriority, kAEDefaultTimeout, (IdleProcPtr)0L,
- (EventFilterProcPtr)0L);
-
- done:
- if (scratchRecord.dataHandle != NULL)
- (void)AEDisposeDesc(&scratchRecord);
- if (insertionLoc.dataHandle != NULL)
- (void)AEDisposeDesc(&insertionLoc);
- if (insertionLoc.dataHandle != NULL)
- (void)AEDisposeDesc(&anAppleEvent);
-
- CheckResult(err, "ae_do_NewDocument");
- }
-
-